//
//  StatusViewController.m
//  LiveEducation
//
//  Created by Liu,Qingcan on 2017/4/12.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "StatusViewController.h"
#import "UpdateRecordApi.h"
#import "UpdateCollectApi.h"
#import "UpdateWebcastApi.h"
#import "User_RoomAllInfoApi.h"
#import "UserModel.h"
#import "RoomModel.h"
#import "LEUserRoomItem.h"

@interface StatusViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign)LEUserStatus userStatus;

@property (nonatomic,strong)UITableView *myTableview;
//@property (nonatomic,strong)NSMutableArray *dataList;
@property (nonatomic,assign)NSInteger userId;

@property (nonatomic,strong)NSMutableArray *statusList;


@end

@implementation StatusViewController

- (instancetype)initWithUserStatus:(LEUserStatus) userStatus{
    self = [super init];
    if (self) {
        self.userStatus = userStatus;
        [self.navigationController setNavigationBarHidden:NO];
        self.navigationController.navigationBar.backgroundColor = [UIColor darkGrayColor];
        self.view.backgroundColor = [UIColor colorWithRed:89/255.0f green:113/255.0f blue:134/255.0f alpha:1.f];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor colorWithRed:89/255.0f green:113/255.0f blue:134/255.0f alpha:1.f];
    [self.view addSubview:self.myTableview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}





#pragma mark delegate
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footer = [[UIView alloc] init];
        //        footer.frame = CGRectMake(0, 0, self.view.bounds.size.width, 30);
        footer.backgroundColor = [UIColor lightGrayColor];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2.f - 150, 10, 300, 30)];
        label.text = @"-----没有更多了-----";
        label.textAlignment = NSTextAlignmentCenter;
        [footer addSubview:label];
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.statusList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"statusCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
        
    }
    LERoomItem *item = self.statusList[indexPath.row];
    cell.textLabel.text = item.title;
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark private
//网络请求
- (void)postNetworkRequest{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL user_login_status = [defaults boolForKey:@"user_login_status"];
    if (!user_login_status) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注意" message:@"请先执行登录操作，才会有数据喔！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES
                         completion:nil];
    }else{
        UserModel *userDB = [[UserModel alloc]init];
        NSArray *arr = [userDB selectNumberOfData:0];
        if (arr.count >0) {
//            [userDB deleteAllUser];
            LEUserItem *userItem = arr.firstObject;
            self.userId = userItem.nid;
        }
        User_RoomAllInfoApi *api = [[User_RoomAllInfoApi alloc]initWithUserId:self.userId roomId:1];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            NSDictionary *dict = request.responseJSONObject;
            //2.判断服务器状态码
            NSArray *categories = [NSArray array];
            NSNumber *status = [dict objectForKey:@"statusCode"];
            if (status.integerValue == 1) {
                categories = dict[@"data"];
                
                RoomModel *roomDB = [[RoomModel alloc]init];
                if(categories== [NSNull null]){
                    return ;
                }
                for(NSDictionary  *dc in categories){
                    LEUserRoomItem *item = [LEUserRoomItem yy_modelWithJSON:dc];
                    if (self.userStatus == LERecordsStatus) {
                        if (item.record_status == 1) {
                            LERoomItem *one= [roomDB selectRoomsByRoomId:item.room_id];
                            [self.statusList addObject:one];
                        }
                        
                    }else if (self.userStatus == LECollectionStatus) {
                        if (item.collection_status == 1) {
                            LERoomItem *one= [roomDB selectRoomsByRoomId:item.room_id];
                            [self.statusList addObject:one];
                        }
                    }else if (self.userStatus == LEMoreStatus) {
                        if ([item.video_url hasPrefix:@"rtmp"]) {
                            LERoomItem *one= [roomDB selectRoomsByRoomId:item.room_id];
                            [self.statusList addObject:one];
                        }
                    }
                    
                }
                
                [self.myTableview reloadData];
                NSLog(@"请求 user--room信息 -- 成功");
            }else{
                //2.输出状态码
                NSLog(@"未能成功获得user--room数据 the reason is : %@",dict[@"statusMsg"]);
            }
            
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            
        }];
    }
    
}


#pragma get

- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:self.view.bounds];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        
        [_myTableview setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        _myTableview.backgroundColor = [UIColor colorWithRed:220/255.0f green:220/255.0f blue:220/255.0f alpha:1.f];//[UIColor whiteColor];
        _myTableview.showsVerticalScrollIndicator = YES;
        _myTableview.showsHorizontalScrollIndicator = NO;
        _myTableview.alwaysBounceVertical = YES;
        _myTableview.contentInset = UIEdgeInsetsMake(5, 3, 3, 3);
        
    }
    return _myTableview;
}


//
//- (NSMutableArray *)dataList{
//    if (!_dataList) {
//        _dataList = [NSMutableArray array];
//    }
//    return _dataList;
//}

- (NSMutableArray *)statusList{
    if (!_statusList) {
        _statusList = [NSMutableArray array];
    }
    return _statusList;
}




#pragma mark set
- (void)setUserStatus:(LEUserStatus)userStatus{
    _userStatus = userStatus;
    [self postNetworkRequest];
    
    
    switch (userStatus) {
        case LERecordsStatus:
            self.title = @"观看记录";
            break;
        case LECollectionStatus:
            self.title = @"收藏记录";
            break;
        case LEMoreStatus:
            self.title = @"直播记录";
            break;
        default:
            break;
    }
    
}





@end
